﻿using iTool.Common;
using iTool.Common.Result;
using Microsoft.AspNetCore.NodeServices;
using Microsoft.Extensions.DependencyInjection;

namespace iTool.Core
{
    /// <summary>
    /// 限定运行权限{读写文件服务等,,,}
    /// </summary>
    public class NodeJS
    {
        static NodeJS() 
        {
            var collection = new ServiceCollection();
            collection.AddNodeServices(option =>
            {
                option.InvocationTimeoutMilliseconds = 3000;
            });

            iBox.RegisterServiceProvider(collection.BuildServiceProvider(), "iNodeService");
        }


        public static async Task<MethodResult<T>> InvokeAsync<T>(string moduleName, params object[] args)
        {
            var result = new MethodResult<T>();

            try
            {
                var nodeServices = iBox.GetService<INodeServices>("iNodeService");
                var data = await nodeServices.InvokeAsync<T>(moduleName, args).ConfigureAwait(false);
                result.IsFinished = true;
                result.Data = data;
            }
            catch (Exception ex)
            {
                result.IsFinished = false;
                result.Discription = ex.Message;
            }

            return result;
        }

        public static async Task<MethodResult<T>> InvokeAsync<T>(CancellationToken cancellationToken, string moduleName, params object[] args)
        {
            var result = new MethodResult<T>();

            try
            {
                var nodeServices = iBox.GetService<INodeServices>("iNodeService");
                var data = await nodeServices.InvokeAsync<T>(cancellationToken, moduleName, args).ConfigureAwait(false);
                result.IsFinished = true;
                result.Data = data;
            }
            catch (Exception ex)
            {
                result.IsFinished = false;
                result.Discription = ex.Message;
            }

            return result;
        }

        public static async Task<MethodResult<T>> InvokeExportAsync<T>(string moduleName, string exportedFunctionName, params object[] args)
        {
            var result = new MethodResult<T>();

            try
            {
                var nodeServices = iBox.GetService<INodeServices>("iNodeService");
                var data = await nodeServices.InvokeExportAsync<T>(moduleName, exportedFunctionName, args).ConfigureAwait(false);
                result.IsFinished = true;
                result.Data = data;
            }
            catch (Exception ex)
            {
                result.IsFinished = false;
                result.Discription = ex.Message;
            }

            return result;
        }

        public static async Task<MethodResult<T>> InvokeExportAsync<T>(CancellationToken cancellationToken, string moduleName, string exportedFunctionName, params object[] args)
        {
            var result = new MethodResult<T>();

            try
            {
                var nodeServices = iBox.GetService<INodeServices>("iNodeService");
                var data = await nodeServices.InvokeExportAsync<T>(cancellationToken, moduleName, exportedFunctionName, args).ConfigureAwait(false);
                result.IsFinished = true;
                result.Data = data;
            }
            catch (Exception ex)
            {
                result.IsFinished = false;
                result.Discription = ex.Message;
            }

            return result;
        }
    }
}
